import {AreaTreeController} from '../../model/areaTree/AreaTreeController';

const areaTreeController = new AreaTreeController();

export const loadArea = function(){
    const key = "AreaTreeData";
    const dataString = localStorage.getItem(key);
    let areaData:Array<any> = [];
    
    let data = {
        version:'',
        areaList:[]
    }
    
    if(dataString){
        data = JSON.parse(dataString);
    }

    return areaTreeController.loadTree({version:data.version}).then((res)=>{
        if(!!res && !!res.areaList && res.areaList.length > 0){
            localStorage.setItem(key, JSON.stringify(res));
            areaData = res.areaList;
        } else {
            areaData = data.areaList;
        }

        return areaData;
    })
}